<?php
namespace app\modules\cn\models;

use app\modules\app\models\CheckAnswer;
use app\modules\exercise\models\ExerciseList as Practise;
use yii\db\ActiveRecord;
use app\libs\Method;
use app\libs\Pager;

class ExerciseResult extends ActiveRecord
{
    public $cateData;

    public static function tableName()
    {
        return '{{%ielts_test_result}}';
    }

    public static function romanNumber($number)
    {
        $letter = $number;
        switch ($number) {
            case "Ⅰ":
            case "ⅰ":
                $letter = "i";
                break;
            case "Ⅱ":
            case "ⅱ":
                $letter = "ii";
                break;
            case "Ⅲ":
            case "ⅲ":
                $letter = "iii";
                break;
            case "Ⅳ":
            case "ⅳ":
                $letter = "iv";
                break;
            case "Ⅴ":
            case "ⅴ":
                $letter = "v";
                break;
            case "Ⅵ":
            case "ⅵ":
                $letter = "vi";
                break;
            case "Ⅶ":
            case "ⅶ":
                $letter = "vii";
                break;
            case "Ⅷ":
            case "ⅷ":
                $letter = "viii";
                break;
            case "Ⅸ":
            case "ⅸ":
                $letter = "ix";
                break;
            case "Ⅹ":
            case "ⅹ":
                $letter = "x";
                break;
            case "Ⅺ":
            case "ⅺ":
                $letter = "xi";
                break;
            case "Ⅻ":
            case "ⅻ":
                $letter = "xii";
                break;
        }
        return $letter;
    }

    //查询用户做题的最新详情
    public static function question()
    {
        $userid = \Yii::$app->session->get('userId');
        if ($userid == false) {
            return null;
        } else {
            $result = self::find()->asArray()->where('userid=' . $userid)->orderBy('id desc')->one();
            $question = $result != false ? ExerciseList::find()->asArray()->select(['content_id', 'pid', 'name', 'cate_id as catId', 'catName', 'cate_id'])->where("cate_id=" . $result['fid'] . " and content_id='" . $result['pid'] . "'")->orderBy('id asc')->one() : array();
            if ($result != false && $question != false) {
                $model = new Practise();
                if ($result['typeid'] == 1) {
                    $data = $model->listenProgressRate([0 => $question], $question['catId']);
                    $data[0]['nameLong'] = $data[0]['name'] ;
                } elseif ($result['typeid'] == 2) {
                    $data = $model->progressRate([0 => $question]);
                    if ($data != false) $data[0]['nameLong'] = strstr($data[0]['name'],'剑雅')==false?$data[0]['catName'] . '-' . $data[0]['name']:$data[0]['name'];
                } else {
                    $question['id']=$question['content_id'];
                    $data = $model->spokenProgressRate([0 => $question], $question['catId']);
                    $data[0]['nameLong'] = $data[0]['name'] ;
                    $data[0]['isCheck']=0;
                    if($userid!=false){
                        $tem=\Yii::$app->db->createCommand("select resultid,id from {{%ielts_test_result}} where fid=" . $question['catId'] . " and pid =" . $data[0]['content_id'] . " and userId=$userid and ISNULL(answer)=0 and LENGTH(trim(answer))>0 group by resultid order by id desc")->queryOne();
                        $checkData= $tem==false?0:CheckAnswer::find()->where('resultId='.$tem['resultid'].' and userid='.$userid.' and typeid=3')->one();
                        $data[0]['isCheck']= $checkData==false?0:1;
                    }
                }
                $return = $data != false ? $data[0] : null;
                $return['typeid'] = $result['typeid'];
                return $return;
            }
            return null;
        }
    }
}
